<!DOCTYPE html>
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
  <meta name="author" content="火星科技 http://mars2d.cn " />
  <meta name="apple-touch-fullscreen" content="yes" />
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  <meta name="format-detection" content="telephone=no" />
  <meta name="x5-fullscreen" content="true" />
  <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
  <!-- 标题及搜索关键字 -->
  <meta name="keywords" content="火星科技,cesium,3D,GIS,marsgis,二维,地图,地球,开发,框架,系统,示例,资料,模型,离线,外包,合肥,安徽,中国" />
  <meta name="description"
        content="火星科技 合肥火星 合肥火星科技 合肥火星科技有限公司 leaflet leaflet框架 leaflet开发 cesium cesium开发 cesium框架 二维 地图 模型  gis marsgis 地图离线 地图开发 地图框架 地图外包 框架 开发 外包  地图离线 二维地图 二维地图 全景漫游 地理信息系统 云GIS 二维GIS GIS平台 WebGIS" />

  <link rel="shortcut icon" type="image/x-icon" href="//mars2d.cn/favicon.ico" />
  <title>图层管理 | Mars2D | 地理信息平台 | 火星科技 | 合肥火星科技有限公司</title>

  <!--第三方lib-->
  <script type="text/javascript" src="../lib/include-lib.js" libpath="../lib/" include="jquery,font-awesome,ztree,bootstrap,layer,haoutil,turf,mars2d"></script>

  <link href="css/style.css" rel="stylesheet" />
</head>

<body class="dark">
  <!--加载前进行操作提示，优化用户体验-->
  <div id="mask" class="signmask" onclick="removeMask()"></div>

  <div id="mars2dContainer" class="mars2d-container"></div>
  <!-- 面板 -->
  <div class="infoview" style="overflow: auto; left: 5px; left: auto; top: 5px;min-width:200px;">
    <ul id="treeOverlays" class="ztree"></ul>
  </div>

  <script src="./js/common.js"></script>

  <script type="text/javascript">
    "use script"; //开发环境建议开启严格模式

    var map;

    function initMap(options) {
      //合并属性参数，可覆盖config.json中的对应配置
      let mapOptions = mars2d.Util.merge(options, {
        center: { lat: 31.623553, lng: 117.322405, alt: 123536, heading: 359, pitch: -81 },
        control: {
          baseLayerPicker: false,
        },
      });

      //创建地图
      map = new mars2d.Map("mars2dContainer", mapOptions);

      initTree();
    }

    //===========================树控件处理============================
    let layers = [];
    let layersObj = {};

    function initTree() {
      //初始化树
      let setting = {
        check: {
          enable: true,
        },
        data: {
          simpleData: {
            enable: true,
          },
        },
        callback: {
          onCheck: treeOverlays_onCheck,
          onDblClick: treeOverlays_onDblClick,
        },
      };

      let zNodes = [];
      layers = map.getLayers({
        basemaps: true, //是否取config.json中的basempas
        layers: true, //是否取config.json中的layers
      });

      //遍历出config.json中所有的basempas和layers
      for (let i = layers.length - 1; i >= 0; i--) {
        let node = _getNodeConfig(layers[i]);
        if (node) {
          zNodes.push(node);
        }
      }

      //
      $.fn.zTree.init($("#treeOverlays"), setting, zNodes);
    }

    function _getNodeConfig(layer) {
      if (layer == null || layer.options.noLayerManage) {
        return;
      }

      let item = layer.options;

      if (!item.name) {
        return;
      }

      let node = {
        id: layer.id,
        pId: layer.pid,
        name: layer.name,
        uuid: layer.uuid,
      };

      // console.log(`${layer.name}:uuid-${layer.uuid}:id-${layer.id}`);

      if (layer.hasEmptyGroup) {
        //空数组
        node.icon = "img/layer/folder.png";
        node.open = item.open == null ? true : item.open;
      } else if (layer.hasChildLayer) {
        //有子节点的数组
        node.icon = "img/layer/layerGroup.png";
        node.open = item.open == null ? true : item.open;
      } else {
        node.icon = "img/layer/layer.png";
        if (layer.parent) {
          node._parentId = layer.parent.uuid;
        }
      }
      //记录图层
      layersObj[node.uuid] = layer;
      return node;
    }

    function treeOverlays_onCheck(e, treeId, chktreeNode) {
      let treeObj = $.fn.zTree.getZTreeObj(treeId);
      //获得所有改变check状态的节点
      let changedNodes = treeObj.getChangeCheckedNodes();

      for (let i = changedNodes.length - 1; i >= 0; i--) {
        let treeNode = changedNodes[i];
        treeNode.checkedOld = treeNode.checked;
        let layer = layersObj[treeNode.uuid];
        if (layer == null) {
          continue;
        }

        //显示隐藏透明度设置view
        if (treeNode.checked) {
          $("#" + treeNode.tId + "_range").show();
        } else {
          $("#" + treeNode.tId + "_range").hide();
        }

        //特殊处理同目录下的单选的互斥的节点，可在config对应图层节点中配置"radio":true即可
        if (layer.options.radio && treeNode.checked) {
          let nodes = treeObj.getNodesByFilter(
            function (node) {
              let item = layersObj[node.uuid];
              return item.options.radio && item.pid == layer.pid && node.uuid != treeNode.uuid;
            },
            false,
            treeNode.getParentNode()
          );
          for (let nidx = 0; nidx < nodes.length; nidx++) {
            nodes[nidx].checkedOld = false;
            treeObj.checkNode(nodes[nidx], false, true);

            $("#" + nodes[nidx].tId + "_range").hide();

            let layertmp = layersObj[nodes[nidx].uuid];
            layertmp.show = false;
          }
        }

        //处理图层显示隐藏
        if (treeNode._parentId) {
          let parentLayer = layersObj[treeNode._parentId];
          if (parentLayer) {
            updateLayerShow(parentLayer, treeNode.checked);
          }
        } else {
          updateLayerShow(layer, treeNode.checked);
        }
      }
    }

    //更新图层:显示隐藏状态
    function updateLayerShow(layer, show) {
      if (show) {
        map.addLayer(layer);
      } else {
        map.removeLayer(layer);
      }
    }

    function treeOverlays_onDblClick(event, treeId, treeNode) {
      if (treeNode == null || treeNode.uuid == null) {
        return;
      }
      let layer = layersObj[treeNode.uuid];
      if (layer && layer.isAdded) {
        layer.flyTo();
      }
    }
  </script>
</body>

</html>
